package com.wuye.controller;

import com.wuye.model.customer.*;
import com.wuye.repository.OptionDetailRepository;
import com.wuye.repository.OptionPollRepository;
import com.wuye.service.CustomerSuggestService;
import com.wuye.web.ST;
import com.wuye.web.SessionHandler;
import com.wuye.web.UserSession;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * Created by zhanchi on 2016/6/29.
 */
@Component
@RequestMapping("/rest")
public class RestarantController{
    private Log log = LogFactory.getLog(this.getClass());
    @Autowired
    private OptionDetailRepository options;
    @Autowired
    private CustomerSuggestService sug;
    @Autowired
    private OptionPollRepository optionPoll;
    @Autowired
    private SessionHandler sessionHandler;

    @RequestMapping("/findSug.jspa")
    @ResponseBody
    public String findSug(){
        List<Custom> lists = null;
        try {
            lists = sug.findCustName();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ST.ok.getJsonObject().put("names",lists).toString();
    }
    @RequestMapping("/findOpinion.jspa")
    @ResponseBody
    public String findOpinion(String cusId,HttpSession session){
        OptionConvert oc = sug.findAllFields(cusId);
        UserSession usersession = sessionHandler.getUserSession(session);
        usersession.setSuggestId(Integer.valueOf(cusId));//客户调查表id
        usersession.setModelId(oc.getId());//模板id
        log.info(oc);
        return ST.ok.getJsonObject(oc).toString();
    }

    @RequestMapping("/saveRecord.jspa")
    public String saveRecord(HttpSession session,String arr){
        JSONObject obj = JSONObject.fromObject(arr);
        log.info(obj);
        obj.get("arr");
        UserSession usersession = sessionHandler.getUserSession(session);
        UserOption userOption = new UserOption();
        Calendar c = Calendar.getInstance();
        SimpleDateFormat formt  =   new SimpleDateFormat("yyyy-MM-dd");
        c  =  Calendar.getInstance(Locale.CHINESE);
        String currenttime= formt.format(c.getTime());
        //设置用户存储信息
        userOption.setUpCusid(usersession.getModelId());
        userOption.setUpSubtime(currenttime);
        userOption.setUpUsername(usersession.getUserName());
        userOption.setTelphone(usersession.getCellPhone());
        userOption.setUpCompany("物业一公司");
        userOption.setIsWritten("1");
        userOption.setIsIn("1");
        userOption.setOtherSuggest(obj.get("other_text").toString());
        return null;
    }
    @RequestMapping("/findAllPoll.jspa")
    public ModelAndView findAllPoll(HttpSession session){
        List<OptionPoll> lists = optionPoll.findAll();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/jspa/suggest/suggestModel");
        mv.addObject("list",lists);
        return mv;
    }
    //删除表单
    @RequestMapping("/deleteRow.jspa")
    @ResponseBody
    public String deletePoll(String pollId){
        optionPoll.delete(pollId);
        return ST.ok.getJsonObject().toString();
    }

    //根据模板id查询该模板的所有属性
    @RequestMapping("/findModelDetail.jspa")
    public ModelAndView findModelDetail(String modelId){
        OptionConvert opt = sug.findPollFields(modelId);
        log.info(opt);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/jspa/suggest/edit");
        mv.addObject("opt",opt);
        return mv;
    }
}
